#!/usr/bin/env bash
set -e

function tmpsed() {
    sed -n "/^#.*\b$1\b/,/^---/p" $HOME/study/gitee/collection/wiki/$2 | sed "/^---$/d" | glow
}

function tmpsed_ks() {
    sed -n "/^#.*$1/,/^---/p" $HOME/study/gitee/collection/wiki/$2 | sed "/^---$/d" | glow
}


if [[ $# == 0 || $1 == "-h" ]]; then
    tmpsed h help.md
    exit
fi

function tmpecho(){
    echo -e "在文件\"\e[33m$2\e[0m\"中，查询小标题与\"\e[33m$1\e[0m\"的相关内容："
}

case $1 in
    h)
        if [[ -z $2  ]]; then
            tmpsed h help.md
            exit
        fi
        shift
        tmpecho $1 help.md
        tmpsed $1 help.md
        ;;
    m)
        shift
        tmpecho $1 markdown.md
        tmpsed $1 markdown.md
        ;;
    bt)
        shift
        sed -n "/^#/p" $HOME/study/gitee/collection/wiki/$1 | glow
        ;;
    *)
        if [[ -z $2 ]]; then
            tmpfile=archlinux_using.md
        else
            tmpfile=$2
        fi
        tmpecho $1 $tmpfile
        tmpsed_ks $1 $tmpfile
        unset tmpfile
        ;;
esac

